/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package model;

import database.DBConnection;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import util.PropertiesManager;

/**
 *
 * @author Igor
 */
public class TagDAO{
    
    private Connection conexao;  
    
    public TagDAO() throws ClassNotFoundException, SQLException {
        DBConnection db = new DBConnection();
        boolean teste = db.openConnection();
        if (teste == true) {
            conexao = db.getConexao();
        }
    }
    
    public TagDAO(Connection conexao) {
        this.conexao = conexao;
    }
        
    public ArrayList pesquisaPorArtista(Artista artista) throws SQLException {
    if (artista != null) {
            String SQL = PropertiesManager.getProperty("selectByArtista.tag");            
            PreparedStatement st = conexao.prepareStatement(SQL);  
            
            st.setInt(1, artista.getIdArtista());
            ResultSet res = st.executeQuery();
               
            ArrayList<String> tags = new ArrayList();
            
            while (res.next()) {
                tags.add(res.getString("tag"));
            }
            return tags;
        }
        return null;
    }
    
    public void inserirByArtista(Artista artista, String tag) throws SQLException{
        if (artista != null && tag !=null){
            String SQL = PropertiesManager.getProperty("insertByArtista.tag");            
            PreparedStatement st = conexao.prepareStatement(SQL);
            System.out.println(artista);
            st.setInt(1, artista.getIdArtista());
            st.setString(2, tag);
            
            st.execute();
        }
    }
    
    public void removeByArtista(Artista artista, String tag) throws SQLException{
        if (artista != null && tag !=null){
            String SQL = PropertiesManager.getProperty("deleteByArtista.tag");            
            PreparedStatement st = conexao.prepareStatement(SQL);
            
            st.setInt(1, artista.getIdArtista());
            st.setString(2, tag);            
            st.execute();
        }
    }
}
    

